package com.bestcem.xm.deliver.grpc.client;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.openplatform.grpc.v1.services.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class OpenPlatformGrpcClient {

    @GrpcClient(GrpcClientNames.DS_OPENPLATFORM)
    private BaseOpenplatformServiceGrpc.BaseOpenplatformServiceBlockingStub stub;

    public ExtParam getExtParam(String id) {
        GetExtParamRequest.Builder builder = GetExtParamRequest.newBuilder();
        builder.setId(id);
        try {
            ExtParamResponse response = stub.getExtParam(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] getExtParam, id = {} Error; ErrorMsg: {}", id, response);
                return null;
            }
            return response.getExtParam();
        } catch (Exception e) {
            log.error("[deliver] ex getExtParam, id = {} Error; ErrorMsg: {}", id, e.getMessage());
            return null;
        }
    }

    public Integer deleteOpenCustomDeliverByDeliverId(String orgId, List<String> ids) {
        if (StrUtil.isBlank(orgId) || CollectionUtils.isEmpty(ids)) {
            log.error("deleteOpenCustomDeliverByDeliverId 参数错误");
            return 0;
        }
        DeleteOpenCustomDeliverByDeliverIdRequest.Builder builder = DeleteOpenCustomDeliverByDeliverIdRequest.newBuilder();
        builder.addAllDeliverIds(ids);
        builder.setOrgId(orgId);
        try {
            DeleteResponse response = stub.deleteOpenCustomDeliverByDeliverId(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] deleteOpenCustomDeliverByDeliverId, deliver_ids = {} Error; ErrorMsg: {}", ids, response);
                return null;
            }
            return response.getCount();
        } catch (Exception e) {
            log.error("[deliver] ex deleteOpenCustomDeliverByDeliverId, deliver_ids = {} Error; ErrorMsg: {}", ids, e.getMessage());
            return null;
        }
    }

    public List<ExtParamEnumManage> listExtParamEnumManage(String orgId, String extParamId) {
        ListExtParamEnumManageRequest.Builder builder = ListExtParamEnumManageRequest.newBuilder();
        ExtParamEnumManage.Builder extParam = ExtParamEnumManage.newBuilder();
        extParam.setOrgId(orgId);
        extParam.setExtParamId(extParamId);
        builder.setExtParamEnumManage(extParam);
        builder.setSearch("");
        builder.addAllStatus(Lists.newArrayList());
//        builder.addAllFields(null);
        try {
            ExtParamEnumManageSearchResponse response = stub.listExtParamEnumManage(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] ListExtParamEnumManage, orgId = {} extParamId = {} Error; ErrorMsg: {}", orgId, extParamId, response);
                return null;
            }
            return response.getExtParamEnumManageListList();
        } catch (Exception e) {
            log.error("[deliver] ex ListExtParamEnumManage, orgId = {} extparamId = {} Error; ErrorMsg: {}", orgId, extParamId, e.getMessage());
            return null;
        }
    }

    public String listEncryptionConfig(String orgId) {
        ListEncryptionConfigRequest.Builder builder = ListEncryptionConfigRequest.newBuilder();
        EncryptionConfig.Builder encryptionConfigBuilder = EncryptionConfig.newBuilder();
        encryptionConfigBuilder.setOrgId(orgId);
        builder.setEncryptionConfig(encryptionConfigBuilder);
        builder.setSearch("");
        builder.addAllStatus(Lists.newArrayList());
//        builder.addAllFields(null);
        try {
            EncryptionConfigSearchResponse response = stub.listEncryptionConfig(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] listEncryptionConfig, orgId = {} Error; ErrorMsg: {}", orgId, response);
                return null;
            }
            List<EncryptionConfig> encryptionConfigList = response.getEncryptionConfigListList();
            return JSONObject.parseObject(encryptionConfigList.get(0).getAesConfig()).getString("aes_key");
        } catch (Exception e) {
            log.error("[deliver] ex listEncryptionConfig, orgId = {}  Error; ErrorMsg: {}", orgId, e.getMessage());
            return null;
        }
    }

    public FunctionOption getFunctionOption(String id) {
        GetFunctionOptionRequest.Builder builder = GetFunctionOptionRequest.newBuilder();
        builder.setId(id);
        try {
            FunctionOptionResponse response = stub.getFunctionOption(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] getFunctionOption, id = {} Error; ErrorMsg: {}", id, response);
                return null;
            }
            return response.getFunctionOption();
        } catch (Exception e) {
            log.error("[deliver] ex getFunctionOption, id = {} Error; ErrorMsg: {}", id, e.getMessage());
            return null;
        }
    }

    public List<ConditionManage> listConditionManage(String code) {
        ListConditionManageRequest.Builder builder = ListConditionManageRequest.newBuilder();
        ConditionManage.Builder conditionManage = ConditionManage.newBuilder();
        conditionManage.setCode(code);
        builder.setConditionManage(conditionManage);
        builder.setSearch("");
        builder.addAllStatus(Lists.newArrayList());
//        builder.addAllFields(null);
        try {
            ConditionManageSearchResponse response = stub.listConditionManage(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] listConditionManage, code = {} Error; ErrorMsg: {}", code, response);
                return null;
            }
            return response.getConditionManageListList();
        } catch (Exception e) {
            log.error("[deliver] ex listConditionManage, code = {} Error; ErrorMsg: {}", code, e.getMessage());
            return null;
        }
    }

    public List<SystemManage> listSystemManage(String code) {
        ListSystemManageRequest.Builder builder = ListSystemManageRequest.newBuilder();
        SystemManage.Builder systemManage = SystemManage.newBuilder();
        systemManage.setCode(code);
        builder.setSystemManage(systemManage);
        builder.setSearch("");
        builder.addAllStatus(Lists.newArrayList());
//        builder.addAllFields(null);
        try {
            SystemManageSearchResponse response = stub.listSystemManage(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[deliver] listSystemManage, code = {} Error; ErrorMsg: {}", code, response);
                return null;
            }
            return response.getSystemManageListList();
        } catch (Exception e) {
            log.error("[deliver] ex listSystemManage, code = {} Error; ErrorMsg: {}", code, e.getMessage());
            return null;
        }
    }
}
